<?php

class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
        //$this->smarty=Zend_Registry::get('smarty');
        $this->_layout = $this->_helper->layout->getLayoutInstance();
        $this->_layout->location='home';
    }
    
    public function indexAction()
    {
        //print_r($_SERVER[PHP_SELF]);
        // action body
       //$this->view->headLink()->prependStylesheet("/public/allcss/global.css");\
       	//答疑区域
        $myNamespace = new Zend_Session_Namespace('Myspace');
        if(Zend_Auth::getInstance()->hasIdentity()&&$myNamespace->userjob=='teacher')//若是老师
        {
            $this->view->userjob = 'teacher';
            $dbapp = new Application_Model_Appiontment();
            $teachername = Zend_Auth::getInstance()->getIdentity();
            $timenow = time() + 8 * 3600;
            $where = "`teacher`=\"$teachername\" and `date`> $timenow and `status`=2";
            $this->view->allapp = $dbapp->fetchAll($where,"date");
        }
        else 
        {
            $this->view->userjob='others';
            $timenow = time() + 8 * 3600;
            $where = "`date`>=$timenow";
            $dbapp = new Application_Model_Appiontment();
            $this->view->allapp = $dbapp->fetchAll($where,'date',7,null);
        }
        
        //活动区域
        $dbact = new Application_Model_Recentact();
        $this->view->allacts = $dbact->fetchAll(null,'date desc',6,null)->toArray();
        
        
        
       //论坛区域
        $dbforum = new Application_Model_Forum();
		$this->view->allforums = $dbforum->fetchAll('`replytoid`=0','time desc',6,null)->toArray();
        
        
        //下载区域
        $dbfile = new Application_Model_File();
        $this->view->allfiles = $dbfile->fetchAll(null,'filedate desc',10,null)->toArray();
    }
	public function registerAction()
	{
	    if(Zend_Auth::getInstance()->hasIdentity())//已登录
	    {
	    	$this->_redirect('/forum/index');
	    }
	    if ($this->getRequest()->isPost()) {
	    	$getdata=$_POST;
	        //echo "收到了！";
	        //print_r($getdata);
	        if($getdata['kindradio']=='teacher')
	        {
	            $this->view->isteacher = true;
	            if(empty($getdata['college']))
	            {
	            	$this->view->tipcollege = '学院不能为空！';
	            	$this->initselect();
	            	return ;
	            }
	            if(empty($getdata['nickname']))
	            {
	            	$this->view->tipnickname = '账号不能为空！';
	            	$this->initselect();
	            	return ;
	            }
	            $teacher=new Application_Model_Teachers();
	            $where = "`nickname`= \"$getdata[nickname]\"";
	            if($teacher->fetchRow($where)!=null)
	            {
	            	$this->view->tipnickname = '该账号已经被注册了';
	            	$this->initselect();
	            	return ;
	            }
	            if(empty($getdata['realname']))
	            {
	            	$this->view->tiprealname = '姓名不能为空！';
	            	$this->initselect();
	            	return ;
	            }
	            if(empty($getdata['mail']))
	            {
	            	$this->view->tipmail = '邮箱不能为空！';
	            	$this->initselect();
	            	return ;
	            }
	            $validator = New Zend_Validate_EmailAddress();
	            if(!$validator->isValid($getdata['mail']))
	            {
	            	$this->view->tipmail = '邮箱格式不正确';
	            	$this->initselect();
	            	return;
	            }
	            $where = "`mail`=\"$getdata[mail]\"";
	            if($teacher->fetchRow($where)!=null)
	            {
	            	$this->view->tipmail = '该邮箱已经被注册了';
	            	$this->initselect();
	            	return;
	            }
	            if(empty($getdata['password']))
	            {
	            	$this->view->tippassword = '密码不能为空！';
	            	$this->initselect();
	            	return ;
	            }
	            if($getdata['confirmpassword']!=$getdata['password'])
	            {
	            	$this->view->tipconfirmpassword = '两次输入的密码不相同';
	            	$this->initselect();
	            	return ;
	            }
	            $data=array('college'=>$getdata["college"],
	            		'nickname'=>$getdata["nickname"],
	            		'realname'=>$getdata['realname'],
	            		'mail'=>$getdata["mail"],
	            		'password'=>md5($getdata["password"]),
	            		'headimg'=>'/images/forum/default.png'
	            );
	             
	            $id = $teacher->insert($data);
	            if($id)
	            {
	            	$this->_redirect('/index/login');
	            }
	            $this->initselect();
	        	return ;	
	        }
	        if(empty($getdata['schoolnumber']))
	        {
	        	$this->view->tipschoolnumber = '学号不能为空！';
	        	$this->initselect();
	        	return ;
	        }
	        if(!is_int(intval($getdata['schoolnumber']))||intval($getdata['schoolnumber'])<1000000000)
	        {
	            $this->view->tipschoolnumber = '学号不正确！';
	            $this->initselect();
	            return ;
	        }
	        if(empty($getdata['major']))
	        {
	        	$this->view->tipmajor = '专业不能为空！';
	        	$this->initselect();
	        	return ;
	        }
	        if(empty($getdata['grade']))
	        {
	        	$this->view->tipgrade = '年级不能为空！';
	        	$this->initselect();
	        	return ;
	        }
	        if(empty($getdata['class']))
	        {
	        	$this->view->tipclass = '班级不能为空！';
	        	$this->initselect();
	        	return ;
	        }
	        if(empty($getdata['college']))
	        {
	        	$this->view->tipcollege = '学院不能为空！';
	        	$this->initselect();
	        	return ;
	        }
	        if(empty($getdata['nickname']))
	        {
	        	$this->view->tipnickname = '账号不能为空！';
	        	$this->initselect();
	        	return ;
	        }
	        $student=new Application_Model_Students();
	        $where = "`nickname`= \"$getdata[nickname]\"";
	        if($student->fetchRow($where)!=null)
	        {
	            $this->view->tipnickname = '该账号已经被注册了';
	            $this->initselect();
	            return ;
	        }
	        if(empty($getdata['realname']))
	        {
	        	$this->view->tiprealname = '姓名不能为空！';
	        	$this->initselect();
	        	return ;
	        }
	        if(empty($getdata['mail']))
	        {
	        	$this->view->tipmail = '邮箱不能为空！';
	        	$this->initselect();
	        	return ;
	        }
	        $validator = New Zend_Validate_EmailAddress();
	        if(!$validator->isValid($getdata['mail']))
	        {
	            $this->view->tipmail = '邮箱格式不正确';
	            $this->initselect();
	            return;
	        }
	        $where = "`mail`=\"$getdata[mail]\"";
	        if($student->fetchRow($where)!=null)
	        {
	            $this->view->tipmail = '该邮箱已经被注册了';
	            $this->initselect();
	            return;
	        }
	        if(empty($getdata['password']))
	        {
	        	$this->view->tippassword = '密码不能为空！';
	        	$this->initselect();
	        	return ;
	        }
	        if($getdata['confirmpassword']!=$getdata['password'])
	        {
	        	$this->view->tipconfirmpassword = '两次输入的密码不相同';
	        	$this->initselect();
	        	return ;
	        }
	        $data=array('schoolnumber'=>intval($getdata["schoolnumber"]),
	               'college'=>$getdata["college"],
	                'major'=>$getdata["major"],
	                'grade'=>$getdata["grade"],
	                'class'=>$getdata["class"],
	                'nickname'=>$getdata["nickname"],
	                'realname'=>$getdata['realname'],
	                'mail'=>$getdata["mail"],
	                'password'=>md5($getdata["password"]),
	                'headimg'=>'/images/forum/default.png'
	                );
	        
	      	$id = $student->insert($data);
	        if($id)
	        {
	            /*echo "注册成功！";
	            echo "<p>";
	            echo "新的记录的id值为:";
				echo $id;
	            echo "<p>";
	            echo "其内容为：";
	            $table = "students";
	            $sql = "select * from $table where `id` = $id";
	            $result = $student->getAdapter()->fetchRow($sql);
	            echo "<p>";
	            foreach($result as $key=>$value)
	            {
	                echo $key;
	                echo "的值为：";
	                echo $value;
	                echo "<p>";
	            }*/
	            $this->_redirect('/index/login');
	        }	        
	    }
	    $collegeconfig = new Zend_Config_Ini(APPLICATION_PATH .'/configs/college.ini','college');
	    $this->view->collegearray = $collegeconfig->toArray();
	    
	}
	public function initselect()
	{
	    $collegeconfig = new Zend_Config_Ini(APPLICATION_PATH .'/configs/college.ini','college');
	    $this->view->collegearray = $collegeconfig->toArray();
	}
	public function teacherregisterAction()
	{
	    if(Zend_Auth::getInstance()->hasIdentity())//已登录
	    {
	    	$this->_redirect('/forum/index');
	    }
	 	if ($this->getRequest()->isPost()) {
	    	$getdata=$_POST;
	    	//print_r($getdata);
	        if(empty($getdata['college']))
	        {
	        	$this->view->tipcollege = '学院不能为空！';
	        	return ;
	        }
	        if(empty($getdata['nickname']))
	        {
	        	$this->view->tipnickname = '账号不能为空！';
	        	return ;
	        }
	        $teacher=new Application_Model_Teachers();
	        $where = "`nickname`= \"$getdata[nickname]\"";
	        if($teacher->fetchRow($where)!=null)
	        {
	            $this->view->tipnickname = '该账号已经被注册了';
	            return ;
	        }
	        if(empty($getdata['realname']))
	        {
	        	$this->view->tiprealname = '姓名不能为空！';
	        	return ;
	        }
	        if(empty($getdata['mail']))
	        {
	        	$this->view->tipmail = '邮箱不能为空！';
	        	return ;
	        }
	        $validator = New Zend_Validate_EmailAddress();
	        if(!$validator->isValid($getdata['mail']))
	        {
	            $this->view->tipmail = '邮箱格式不正确';
	            return;
	        }
	        $where = "`mail`=\"$getdata[mail]\"";
	        if($teacher->fetchRow($where)!=null)
	        {
	            $this->view->tipmail = '该邮箱已经被注册了';
	            return;
	        }
	        if(empty($getdata['password']))
	        {
	        	$this->view->tippassword = '密码不能为空！';
	        	return ;
	        }
	        if($getdata['confirmpassword']!=$getdata['password'])
	        {
	        	$this->view->tipconfirmpassword = '两次输入的密码不相同';
	        	return ;
	        }
	        $data=array('college'=>$getdata["college"],
	                'nickname'=>$getdata["nickname"],
	                'realname'=>$getdata['realname'],
	                'mail'=>$getdata["mail"],
	                'password'=>md5($getdata["password"]),
	                'headimg'=>'/images/forum/default.png'
	                );
	        
	      	$id = $teacher->insert($data);
	        if($id)
	        {
	            $this->_redirect('/index/login');
	        }	        
	    }
	    $collegeconfig = new Zend_Config_Ini(APPLICATION_PATH .'/configs/college.ini','college');
	    $this->view->collegearray = $collegeconfig->toArray();
	}
	public function loginAction()
	{
	    $this->view->tip = '';
	    if(isset($_GET['fromhref']))
	    {
	        $this->view->fromhref =$_GET['fromhref'];
	    }
	    //print_r('来自'.$this->view->fromhref);
	    if($this->getRequest()->isPost())
	    {
	        Zend_Loader::loadClass('Zend_Filter_StripTags');
	        $filter = new Zend_Filter_StripTags();
	        $username = $filter->filter($this->_request->getPost('nickname'));
	        $password = $filter->filter($this->_request->getPost('password'));
	        $password = md5($password);
	         
	        if (empty($username))
	        {
	        	$this->view->tip = '请输入账号！！';
	        	$this->view->logintip = '请输入账号！！';
	        }
	        else
	        {
	            Zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');
	            $result;
	            
	            if($this->getRequest()->getParam('loginmethod')=='teacher')
	            {
	            	$authAdapter = new Zend_Auth_Adapter_DbTable(Application_Model_Teachers::getDefaultAdapter());
	                $authAdapter->setTableName('teachers');
	            	$authAdapter->setIdentityColumn('nickname');
	            	$authAdapter->setCredentialColumn('password');
	            	$authAdapter->setIdentity($username);
	            	$authAdapter->setCredential($password);
	            	$auth = Zend_Auth::getInstance();
	            	$result = $auth->authenticate($authAdapter);
	            }
	            else 
	            {
	                $authAdapter = new Zend_Auth_Adapter_DbTable(Application_Model_Students::getDefaultAdapter());
	                /*$table  = "students";
	                $re =  Application_Model_Students::getDefaultAdapter()->fetchRow("select * from $table where `nickname`= \"$username\"");
	                foreach($re as $key=>$value)
	                {
	                    echo $key;
	                    echo '的值为：';
	                    echo $value;
	                    echo '</br>';
	                }*/
	                $authAdapter->setTableName('students');
	                $authAdapter->setIdentityColumn('nickname');
	                $authAdapter->setCredentialColumn('password');
	                //echo '接收到的名字为：'.$username.'</br>';
	                //echo '接收到的密码为：'.$password.'</br>';
	                $authAdapter->setIdentity($username);
	                $authAdapter->setCredential($password);
	                $auth = Zend_Auth::getInstance();
	                $result = $auth->authenticate($authAdapter);
	            }
	            if($result->isValid())
	            {
	                //echo "登录成功";
	                //$this->view->username;
	                //$userjob[] = $this->getRequest()->getParam('loginmethod');
	                //$userjob = array('userjob'=>'teacher','userhehe'=>'hehe');
	                //Zend_Registry::set('userjob',$userjob);
	                //Zend_Registry::getInstance()->set('userjob', $userjob);
	                //$registry = Zend_Registry::getInstance();
	                //$registry->set('userjob','teacher');
	                /*if(Zend_Registry::isRegistered('userjob'))
	                {
	                    //print_r($registry);
	                    global $userjob;
	                    $this->_redirect('/index/index');
	                }*/
	                //global $_userjob;
	                //$GLOBALS['userjob'] = 'teacher';// $this->getRequest()->getParam('loginmethod');
	                
	                $myNamespace = new Zend_Session_Namespace('Myspace');
	                if(!isset($myNamespace->userjob))
	                    $myNamespace->userjob = $this->getRequest()->getParam('loginmethod');
	                //$this->_redirect('/index/index');
	                $tohref = $_GET['fromhref'];
	                
	                $this->_redirect($tohref);
	                
	                //if(empty($tohref))$tohref="index/index";
	                //print_r('tohref:--'.$tohref);
	            }  
	            else
	            {
	                $this->view->tip = '您输入的账号或者密码不正确';
	                $this->view->logintip = '您输入的账号或者密码不正确';
	            }
	            if(isset($_GET['fromhref']))
	            {
	                	$this->_redirect($_GET['fromhref']);
	            }
	        }
	    }
	    
	}
	public function forgetpasswordAction()
	{
		//
	}
	public function jsdlAction()
	{
	    //
	}
	public function logoutAction()
	{
	    //
	    $myNamespace = new Zend_Session_Namespace('Myspace');
	    if(isset($myNamespace->userjob))
	        $myNamespace->unsetAll();
	    Zend_Auth::getInstance()->clearIdentity();
	    if(isset($_GET['fromhref']))
	    {
	        $this->_redirect($_GET['fromhref']);
	    }
	    $this->_redirect('/index/index');
	}

}

